import 'package:flutter/material.dart';

/// 基础的dialog 布局如下
/// ===================
///       title
///       content
///   buttonLeft buttonRight
/// ===================
class SimpleConfirmOrCancelDialog extends StatelessWidget {
  /// 标题文件控件
  final String title;

  /// 原dialog对应参数
  final EdgeInsetsGeometry titlePadding;
  final EdgeInsetsGeometry contentPadding;
  final EdgeInsets insetPadding;

  /// 白色容器对应的radius
  final double containerRadius;

  /// 需要按钮组
  final bool needButtonGroup;

  /// content组件
  final List<Widget> children;

  /// 左右按钮的文字
  final String leftButtonName;
  final String rightButtonName;

  /// 标题文字样式
  final TextStyle titleTextStyle;

  /// 左右文字的样式
  final TextStyle leftTextStyle;
  final TextStyle rightTextStyle;

  /// 左右按钮的点击事件
  final VoidCallback? onLeftButtonClickListener;
  final VoidCallback? onRightButtonClickListener;

  SimpleConfirmOrCancelDialog({
    Key? key,
    required this.title,
    this.titlePadding = const EdgeInsets.fromLTRB(20.0, 16.0, 16.0, 0),
    this.contentPadding = const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 21),
    this.insetPadding = const EdgeInsets.symmetric(horizontal: 44.0, vertical: 24.0),
    this.containerRadius = 5.0,
    this.children = const [],
    this.needButtonGroup = true,
    this.leftButtonName = "退出",
    this.rightButtonName = "确认",
    this.onLeftButtonClickListener,
    this.onRightButtonClickListener,
    this.titleTextStyle = const TextStyle(fontSize: 17, color: Color(0xFF3A3C41), fontWeight: FontWeight.bold),
    this.leftTextStyle = const TextStyle(fontSize: 14, color: Color(0xFFAAAAAA), fontWeight: FontWeight.normal),
    this.rightTextStyle = const TextStyle(fontSize: 14, color: Colors.blueAccent, fontWeight: FontWeight.normal),
  }) : super(key: key) {
    if (needButtonGroup) {
      children.addAll(_buildButtonGroup);
    }
  }

  /// 构建按钮组
  List<Widget> get _buildButtonGroup {
    return [
      const Divider(color: Color(0xFFC7CDD8), height: 0.5),
      SizedBox(
        height: 40,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: GestureDetector(
                onTap: onLeftButtonClickListener,
                child: Container(
                  width: double.infinity,
                  height: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: containerRadius > 0
                        ? BorderRadius.only(
                            bottomLeft: Radius.circular(containerRadius),
                          )
                        : null,
                  ),
                  alignment: Alignment.center,
                  child: Text(leftButtonName, style: leftTextStyle),
                ),
              ),
            ),
            Container(width: 0.5, color: Color(0xFFC7CDD8)),
            Expanded(
              child: GestureDetector(
                onTap: onRightButtonClickListener,
                child: Container(
                  width: double.infinity,
                  height: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: containerRadius > 0
                        ? BorderRadius.only(
                            bottomRight: Radius.circular(containerRadius),
                          )
                        : null,
                  ),
                  alignment: Alignment.center,
                  child: Text(rightButtonName, style: rightTextStyle),
                ),
              ),
            ),
          ],
        ),
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return SimpleDialog(
      titlePadding: titlePadding,
      contentPadding: contentPadding,
      insetPadding: insetPadding,
      title: Text(title, textAlign: TextAlign.center),
      titleTextStyle: titleTextStyle,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(containerRadius))),
      children: children,
    );
  }
}
